﻿#include "jiuxing_rope_coiler_failure.h"

// 使用 utf8 编码
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

// 玖行 卷绳器失效

/// <summary>
/// 
/// </summary>
jiuxing_rope_coiler_failure::jiuxing_rope_coiler_failure()
{
	m_pSteelDet = nullptr;
	m_pCableDet = nullptr;
}


/// <summary>
/// 
/// </summary>
jiuxing_rope_coiler_failure::~jiuxing_rope_coiler_failure()
{
	if (m_pSteelDet != nullptr)
	{
		m_pSteelDet->Release();
		m_pSteelDet = nullptr;
	}

	if (m_pCableDet != nullptr)
	{
		m_pCableDet->Release();
		m_pCableDet = nullptr;
	}

}


/// <summary>
/// 
/// </summary>
/// <returns></returns>
void jiuxing_rope_coiler_failure::init()
{
	m_pSteelDet = CreateDetObj(m_strJX_SteelDetModelPath);
	m_pCableDet = CreateDetObj(m_strJX_CableDetModelPath);
}


/// <summary>
/// 
/// </summary>
/// <param name="pData"></param>
void jiuxing_rope_coiler_failure::execute(QSharedPointer<IMG_DATA> pData, int nBuffersize)
{
	IMG_DATA data = *pData;
	data.image = urlToPixmap(data.strUrl);
	saveImage(data);

	cv::Mat image = QImage2cvMat(data.image);
	int nDispId = data.nDispId;

	if (!image.data || m_pCableDet == nullptr || m_pSteelDet == nullptr)
	{
		return;
	}
	DetectResult res_tmp;

	cv::Mat image_tmp;
	int x_offset = 0;
	int y_offset = 0;
	{
		std::vector<DetectResult> _results;
		m_pSteelDet->Evaluate(image, _results);
		if (_results.size() != 0)
		{
			DetectResult res = _results[0];
			cv::Rect rect = res.box;
			res_tmp = res;
			rect &= cv::Rect(0, 0, image.cols, image.rows);//求交集

			int x1 = rect.tl().x;
			int y1 = rect.tl().y;
			int x2 = rect.br().x;
			int y2 = rect.br().y;
			int x = (x2 - x1) * 0.5 + x1;
			int w = 900;
			cv::Rect rect1(x - w / 2, 0, w, y2);

			image_tmp = image(rect1);


			x_offset = rect1.x;
			y_offset = rect1.y;

		}
	}


	std::vector<DetectResult> results;
	if (image_tmp.data )
	{
		
		m_pCableDet->Evaluate(image_tmp, results);
		// 保存图片
		saveImage(image_tmp, "玖行卷绳器(定位)", data, results.size());
		
		for (int i = 0; i < results.size(); i++)
		{
			DetectResult res = results[i];
			cv::Rect rect = res.box;
			rect &= cv::Rect(0, 0, image_tmp.cols, image_tmp.rows);//求交集

			int x1 = rect.tl().x;
			int y1 = rect.tl().y;
			int x2 = rect.br().x;
			int y2 = rect.br().y;
			int x = (x2 - x1) * 0.5 + x1;
			int w = 200;
			cv::Rect rect1(x - w / 2, 0, w, y2);

			cv::Mat image_roi = image_tmp(rect1);

			std::vector<DetectResult> def_results;

			// 保存图片
			saveImage(image_roi, "玖行卷绳器失效", data, def_results.size());

			
			// 			if (m_pRectDefect != nullptr)
			// 			{
			// 				m_pRectDefect->Evaluate(image_roi, def_results);
			// 			}

			// 如果没检测到破损，则不发布破损相关的数据 
			if (def_results.size() != 0)
			{
				// 返回结果到 kafka 
				_tagResults tagResults;
				tagResults.setImageData(data);
				tagResults.setResults("电缆失效", def_results, res.box.x + x_offset, res.box.y + y_offset);
				tagResults.setVersion("");

				QString strResultTopc = data.strTopicName + suffix_2;
				QString strResultData = tagResults.toJSon();

				pushKafka(strResultTopc, strResultData, data.strStationId);
			}


			QPainter painter(&data.image);
			painter.setBrush(QBrush(QColor(255, 0, 0, 50)));

			QPen pen(Qt::red);
			pen.setWidth(5);
			painter.setPen(pen);

			cv::Point point = getCenterPoint(rect);
			painter.drawRect(rect.tl().x + x_offset, rect.tl().y + y_offset, rect.width, rect.height);

			emit updateShow(data.nDispId, data.image, nBuffersize);
		}

	}


	// 返回结果到 kafka 
	_tagResults tagResults;
	tagResults.setImageData(data);

	tagResults.setResults("电缆", results, x_offset, y_offset);
	tagResults.setVersion(m_strJX_CableDetModelPath);


	QString strResultTopc = data.strTopicName + suffix_1;
	QString strResultData = tagResults.toJSon();

	pushKafka(strResultTopc, strResultData, data.strStationId);
}
